﻿Public Class clsTaskStep
    Public Function SelectTaskStep1(lhj As String, task_step_status As String) As DataTable
        Dim szSql As String = ""
        szSql &= "select agv.task.type,agv.task.start_time,agv.task.end_time,agv.task.tm,agv.task_step.* from agv.task"
        szSql &= " left join agv.task_step on agv.task.sys_id=agv.task_step.task_id"
        szSql &= " where agv.task_step.lhj=? and agv.task_step.status=? and agv.task_step.previous_id is null"
        szSql &= " order by agv.task.sys_id desc "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, lhj, task_step_status)
        If IsNothing(dt) Then
            SelectTaskStep1 = New DataTable
        Else
            SelectTaskStep1 = dt
        End If
    End Function

    Public Function SelectTaskStep2(id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "select * from agv.task_step "
        szSql &= " where previous_id=? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, id)
        If IsNothing(dt) Then
            SelectTaskStep2 = New DataTable
        Else
            SelectTaskStep2 = dt
        End If
    End Function

    Public Function updateTaskStepInto(state As String, state_task As String, id As String, task_id As String,
                                   prod_id As String, gyh_id As String, project_no As String, pgd_id As String, lhj As String, prod_spec_id As String,
                                   jlzl As Decimal, oper As String, bz As Integer, lh_pgd_id As String, jp_type As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            '更新新任务步骤状态
            szSql &= "update agv.task_step set status = ?, update_time = (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= "where sys_id = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, state, id)
            '插入state表状态
            szSql = String.Empty
            szSql &= "insert into agv.task_step_state(task_step_id,state,update_time) values ('" & id & "','" & state & "',(SELECT CURRENT_TIMESTAMP(6)))"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1)
            '更新任务表状态
            szSql = String.Empty
            szSql &= "update agv.task set status = ?,update_time = (SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id=?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, state_task, task_id)

            szSql = String.Empty
            szSql &= "select * from sf.t_mst_mj_details where lhj_id = ? "
            Dim dtrtas As DataTable = DBacs.ExecuteSql(szSql, -1, lhj)
            If dtrtas.Rows.Count = 1 Then
                szSql = String.Empty
                'szSql &= "insert into sf.b_prod_info(factory_id,prod_id,pgd_id,steel_seal_no,project_no,jstz_wt,in_vu_ope,in_vu_bz,mould_no,lhj_id,status,prod_spec_id,into_vu_time,update_time,b_pgd_lh_details_sys_id,jp_type)"
                'szSql &= " values ('TS','" & prod_id & "','" & pgd_id & "','" & gyh_id & "','" & project_no & "','" & jlzl & "','" & oper & "','" & bz & "','" & dtrtas.Rows(0)("mould_no").ToString() & "','" & lhj & "','5','" & prod_spec_id & "',(SELECT CURRENT_TIMESTAMP(6)),(SELECT CURRENT_TIMESTAMP(6)),'" & lh_pgd_id & "','" & jp_type & "')"
                'rtn = DBacs.ExecuteUpdateSql(szSql, -1)
                szSql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
                szSql &= "status=?,lhj_id=?,project_no=?,into_vu_time =(SELECT CURRENT_TIMESTAMP(6)) ,in_vu_ope=?,in_vu_bz=?,mould_no=?,b_pgd_lh_details_sys_id=?"
                szSql &= "where factory_id = ? and prod_id = ?"
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "5", lhj, project_no, oper, bz, dtrtas.Rows(0)("mould_no").ToString(), lh_pgd_id, "TS", prod_id)

            Else
                szSql = String.Empty
                'szSql &= "insert into sf.b_prod_info(factory_id,prod_id,pgd_id,steel_seal_no,project_no,jstz_wt,in_vu_ope,in_vu_bz,lhj_id,status,prod_spec_id,into_vu_time,update_time,b_pgd_lh_details_sys_id,jp_type)"
                'szSql &= " values ('TS','" & prod_id & "','" & pgd_id & "','" & gyh_id & "','" & project_no & "','" & jlzl & "','" & oper & "','" & bz & "','" & lhj & "','5','" & prod_spec_id & "',(SELECT CURRENT_TIMESTAMP(6)),(SELECT CURRENT_TIMESTAMP(6)),'" & lh_pgd_id & "','" & jp_type & "')"
                'rtn = DBacs.ExecuteUpdateSql(szSql, -1)
                szSql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
                szSql &= "status=?,lhj_id=?,project_no=?,into_vu_time =(SELECT CURRENT_TIMESTAMP(6)) ,in_vu_ope=?,in_vu_bz=?,b_pgd_lh_details_sys_id=?"
                szSql &= "where factory_id = ? and prod_id = ?"
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "5", lhj, project_no, oper, bz, lh_pgd_id, "TS", prod_id)

            End If
            szSql = String.Empty
            szSql &= "insert into sf.b_prod_pgd_info(factory_id,prod_id,pgd_id,workshop_id)"
            szSql &= " values ('TS','" & prod_id & "','" & pgd_id & "','3')"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1)
            szSql = String.Empty
            szSql &= " UPDATE sf.t_hw_setting "
            szSql &= " SET status = ? , prod_id = ?, prod_spec_id = ?,last_minutes = ?, update_time= (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= " WHERE "
            szSql &= " factory_id = ? and workshop_id = ? and equip_type_id = ? and equip_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", prod_id, prod_spec_id, 0, "TS", "LH", "LHJ", lhj)

            'szSql = String.Empty
            'szSql &= "update agv.task_step set prod_id=? where sys_id = ?"
            'rtn = DBacs.ExecuteUpdateSql(szSql, -1, prod_id, id)

            szSql = String.Empty
            szSql &= "select * from sf.b_pgd_lh_details where sys_id = ? "
            Dim dtrt As DataTable = DBacs.ExecuteSql(szSql, -1, lh_pgd_id)
            If dtrt.Rows.Count > 0 Then
                If (dtrt.Rows(0)("oper_count") - dtrt.Rows(0)("lhj_finish_count")) > 0 Then
                    '更新生产计划
                    szSql = String.Empty
                    szSql &= "update sf.b_pgd_lh_details set lhj_finish_count = lhj_finish_count + 1 where sys_id = ? "
                    rtn = DBacs.ExecuteUpdateSql(szSql, -1, lh_pgd_id)
                End If
            End If
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function updateTaskStepOut(state As String, state_task As String,
                                   id As String, task_id As String, prod_id As String,
                                   jlzl As Decimal, oper As String, bz As Integer, lhj As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            '更新新任务步骤状态
            szSql &= "update agv.task_step set status = ?, update_time = (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= "where sys_id = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, state, id)
            '插入state表状态
            szSql = String.Empty
            szSql &= "insert into agv.task_step_state(task_step_id,state,update_time) values ('" & id & "','" & state & "',(SELECT CURRENT_TIMESTAMP(6)))"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1)
            '更新任务表状态
            szSql = String.Empty
            szSql &= "update agv.task set status = ?,update_time = (SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id=?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, state_task, task_id)
            '变更制品的状态信息（出炉时间，出炉作业员)
            szSql = String.Empty
            szSql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
            szSql &= " status=?,comp_time =(SELECT CURRENT_TIMESTAMP(6)),out_vu_ope = ?,out_vu_bz = ?, yj_wt=? "
            szSql &= " where factory_id = ? and prod_id = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "6", oper, bz, jlzl, "TS", prod_id)

            szSql = String.Empty
            szSql &= " UPDATE sf.t_hw_setting "
            szSql &= " SET status = ? , prod_id = ? ,prod_spec_id = ? ,last_minutes = ?, update_time= (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= " WHERE factory_id = ? and workshop_id = ? and equip_type_id = ? and equip_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "0", "", "", 0, "TS", "LH", "LHJ", lhj)


            szSql = String.Empty
            szSql &= "select * from sf.b_prod_info where factory_id = ? and prod_id = ? "
            Dim dtrt As DataTable = DBacs.ExecuteSql(szSql, -1, "TS", prod_id)
            If dtrt.Rows.Count > 0 Then
                'If (dtrt.Rows(0)("oper_count") - dtrt.Rows(0)("lhj_finish_count")) > 0 Then
                '更新生产计划
                szSql = String.Empty
                szSql &= "update sf.b_pgd_lh_details set lhj_finish_count = lhj_finish_count + 1 where sys_id = ? "
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, dtrt.Rows(0)("b_pgd_lh_details_sys_id").ToString())

                szSql = String.Empty
                szSql &= "select * from sf.b_pgd_lh_details where factory_id = ? and sys_id = ? "
                Dim dtrtx As DataTable = DBacs.ExecuteSql(szSql, -1, "TS", dtrt.Rows(0)("b_pgd_lh_details_sys_id").ToString())

                szSql = String.Empty
                szSql &= "update sf.b_pgd_mes set finish_count = finish_count + 1 where sys_id = ? "
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, dtrtx.Rows(0)("b_pgd_mes_sys_id").ToString())
                'End If
            End If
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function updateInto(prod_id As String, gyh_id As String, project_no As String, pgd_id As String, lhj As String, prod_spec_id As String,
                                   jlzl As Decimal, oper As String, bz As Integer, lh_pgd_id As String, jp_type As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try

            szSql = String.Empty
            szSql &= "select * from sf.t_mst_mj_details where lhj_id = ? "
            Dim dtrtas As DataTable = DBacs.ExecuteSql(szSql, -1, lhj)
            If dtrtas.Rows.Count = 1 Then
                '入炉
                szSql = String.Empty
                'szSql &= "insert into sf.b_prod_info(factory_id,prod_id,pgd_id,steel_seal_no,project_no,jstz_wt,in_vu_ope,in_vu_bz,mould_no,lhj_id,status,prod_spec_id,into_vu_time,update_time,b_pgd_lh_details_sys_id,jp_type) "
                'szSql &= " values ('TS','" & prod_id & "','" & pgd_id & "','" & gyh_id & "','" & project_no & "','" & jlzl & "','" & oper & "','" & bz & "','" & dtrtas.Rows(0)("mould_no").ToString() & "','" & lhj & "','5','" & prod_spec_id & "',(SELECT CURRENT_TIMESTAMP(6)),(SELECT CURRENT_TIMESTAMP(6)),'" & lh_pgd_id & "','" & jp_type & "')"
                'rtn = DBacs.ExecuteUpdateSql(szSql, -1)
                szSql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
                szSql &= "status=?,lhj_id=?,project_no=?,into_vu_time =(SELECT CURRENT_TIMESTAMP(6)) ,in_vu_ope=?,in_vu_bz=?,mould_no=?,b_pgd_lh_details_sys_id=?"
                szSql &= "where factory_id = ? and prod_id = ?"
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "5", lhj, project_no, oper, bz, dtrtas.Rows(0)("mould_no").ToString(), lh_pgd_id, "TS", prod_id)

            Else
                '入炉
                szSql = String.Empty
                'szSql &= "insert into sf.b_prod_info(factory_id,prod_id,pgd_id,steel_seal_no,project_no,jstz_wt,in_vu_ope,in_vu_bz,lhj_id,status,prod_spec_id,into_vu_time,update_time,b_pgd_lh_details_sys_id,jp_type) "
                'szSql &= " values ('TS','" & prod_id & "','" & pgd_id & "','" & gyh_id & "','" & project_no & "','" & jlzl & "','" & oper & "','" & bz & "','" & lhj & "','5','" & prod_spec_id & "',(SELECT CURRENT_TIMESTAMP(6)),(SELECT CURRENT_TIMESTAMP(6)),'" & lh_pgd_id & "','" & jp_type & "')"
                'rtn = DBacs.ExecuteUpdateSql(szSql, -1)
                szSql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
                szSql &= "status=?,lhj_id=?,project_no=?,into_vu_time =(SELECT CURRENT_TIMESTAMP(6)) ,in_vu_ope=?,in_vu_bz=?,b_pgd_lh_details_sys_id=?"
                szSql &= "where factory_id = ? and prod_id = ?"
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "5", lhj, project_no, oper, bz, lh_pgd_id, "TS", prod_id)

            End If
            szSql = String.Empty
            szSql &= "insert into sf.b_prod_pgd_info(factory_id,prod_id,pgd_id,workshop_id)"
            szSql &= " values ('TS','" & prod_id & "','" & pgd_id & "','3')"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1)
            szSql = String.Empty
            szSql &= " UPDATE sf.t_hw_setting "
            szSql &= " SET status = ? , prod_id = ?, prod_spec_id = ?,last_minutes = ?, update_time= (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= " WHERE "
            szSql &= " factory_id = ? and workshop_id = ? and equip_type_id = ? and equip_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", prod_id, prod_spec_id, 0, "TS", "LH", "LHJ", lhj)

            szSql = String.Empty
            szSql &= "select * from sf.b_pgd_lh_details where sys_id =  ?  "
            Dim dtrt As DataTable = DBacs.ExecuteSql(szSql, -1, lh_pgd_id)
            If dtrt.Rows.Count > 0 Then
                If (dtrt.Rows(0)("oper_count") - dtrt.Rows(0)("lhj_finish_count")) > 0 Then
                    '更新生产计划
                    szSql = String.Empty
                    szSql &= "update sf.b_pgd_lh_details set "
                    szSql &= " lhj_finish_count = lhj_finish_count + 1 "
                    szSql &= " where sys_id = ? "
                    rtn = DBacs.ExecuteUpdateSql(szSql, -1, dtrt.Rows(0)("sys_id"))
                    'szSql = String.Empty
                    'szSql &= "update sf.b_pgd_mes set "
                    'szSql &= " finish_count = finish_count + 1 "
                    'szSql &= " where sys_id = ? "
                    'rtn = DBacs.ExecuteUpdateSql(szSql, -1, dtrt.Rows(0)("b_pgd_mes_sys_id"))
                End If
            End If
            szSql = String.Empty
            szSql &= "SELECT * from agv.task where lhj = ? "
            Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, lhj)
            If dt.Rows.Count > 0 Then
                For index = 0 To dt.Rows.Count - 1
                    If dt.Rows(index)("status").ToString().Contains("待创建") Then
                        szSql = String.Empty
                        szSql &= "update agv.task set status = ? where sys_id= ? "
                        rtn = DBacs.ExecuteUpdateSql(szSql, -1, "已取消", dt.Rows(index)("sys_id").ToString())
                    End If
                Next
            End If
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function updateOut(prod_id As String, lhj As String,
                                   jlzl As Decimal, oper As String, bz As Integer) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            '变更制品的状态信息（出炉时间，出炉作业员)
            szSql = String.Empty
            szSql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
            szSql &= " status=?,comp_time =(SELECT CURRENT_TIMESTAMP(6)),out_vu_ope = ?,out_vu_bz = ?, yj_wt=? "
            szSql &= " where factory_id = ? and prod_id = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "6", oper, bz, jlzl, "TS", prod_id)

            szSql = String.Empty
            szSql &= " UPDATE sf.t_hw_setting "
            szSql &= " SET status = ? , prod_id = ? ,last_minutes = ?, update_time= (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= " WHERE factory_id = ? and workshop_id = ? and equip_type_id = ? and equip_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "0", "", 0, "TS", "LH", "LHJ", lhj)
            'szSql = String.Empty
            'szSql &= "select * from sf.b_prod_info where factory_id = ? and prod_id = ? "
            'Dim dtrt As DataTable = DBacs.ExecuteSql(szSql, -1, "TS", prod_id)
            'If dtrt.Rows.Count > 0 Then
            '    If String.IsNullOrEmpty(dtrt.Rows(0)("b_pgd_lh_details_sys_id").ToString()) = False Then
            '        szSql = String.Empty
            '        szSql &= "select * from sf.b_pgd_lh_details where sys_id  = ? "
            '        Dim dtrt1 As DataTable = DBacs.ExecuteSql(szSql, -1, dtrt.Rows(0)("b_pgd_lh_details_sys_id").ToString())
            '        If (Integer.Parse(dtrt1.Rows(0)("oper_count").ToString()) - Integer.Parse(dtrt1.Rows(0)("lhj_finish_count").ToString())) > 0 Then
            '            '更新生产计划
            '            szSql = String.Empty
            '            szSql &= "update sf.b_pgd_lh_details set lhj_finish_count = lhj_finish_count + 1 where sys_id = ? "
            '            rtn = DBacs.ExecuteUpdateSql(szSql, -1, dtrt.Rows(0)("b_pgd_lh_details_sys_id").ToString())
            '        End If
            '    End If

            'End If
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function
End Class
